package com.todod.base.module.infra.service.baseofficialletter;

import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.module.infra.api.baseofficialletter.BaseOfficialLetterApi;
import com.todod.base.module.infra.dal.dataobject.baseofficialletter.BaseOfficialLetterDO;
import com.todod.base.module.infra.dal.dataobject.baseofficialletter.BaseOfficialLetterDetailDO;
import com.todod.base.module.infra.dal.dataobject.baseofficialletter.BaseOfficialLetterFileDO;
import com.todod.base.module.infra.dal.dataobject.file.FileDO;
import com.todod.base.module.infra.dal.mysql.baseofficialletter.BaseOfficialLetterDetailMapper;
import com.todod.base.module.infra.dal.mysql.baseofficialletter.BaseOfficialLetterFileMapper;
import com.todod.base.module.infra.dal.mysql.baseofficialletter.BaseOfficialLetterMapper;
import com.todod.base.module.infra.dal.mysql.file.FileMapper;
import com.todod.base.module.infra.vo.baseofficialletter.BaseOfficialLetterApiReqVO;
import com.todod.base.module.infra.vo.baseofficialletter.FileRespVO;
import com.todod.base.module.infra.vo.baseofficialletter.UserRespVO;
import com.todod.base.module.system.api.user.AdminUserApi;
import com.todod.base.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.todod.base.framework.common.pojo.CommonResult.success;

@RestController
@Validated
public class BaseOfficialLetterApiImpl implements BaseOfficialLetterApi {

    @Resource
    private BaseOfficialLetterService baseOfficialLetterService;
    @Resource
    private BaseOfficialLetterMapper baseOfficialLetterMapper;
    @Resource
    private BaseOfficialLetterDetailMapper baseOfficialLetterDetailMapper;
    @Resource
    private BaseOfficialLetterFileMapper baseOfficialLetterFileMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private FileMapper fileMapper;

    @Override
    public void setStatus(Long id, Integer bpmStatus) {
        BaseOfficialLetterDO order = baseOfficialLetterService.getBaseOfficialLetter(id);

        switch (bpmStatus) {
            case 2://通过
                order.setStatus(2);
                //修改状态
                baseOfficialLetterMapper.updateById(order);
                break;
            case 3://未通过
                order.setStatus(3);
                baseOfficialLetterMapper.updateById(order);
                break;
        }
    }

    @Override
    public CommonResult<BaseOfficialLetterApiReqVO> getBaseOfficialLetter(Long id) {

        BaseOfficialLetterDO orderSignup = baseOfficialLetterService.getBaseOfficialLetter(id);
        BaseOfficialLetterApiReqVO reqVO = new BaseOfficialLetterApiReqVO();

        BeanUtils.copyProperties(orderSignup, reqVO);
        return null;
    }

    @Override
    public CommonResult<List<UserRespVO>> getLetterUserList(Long officialLetterId) {
        List<BaseOfficialLetterDetailDO> pageResult = baseOfficialLetterDetailMapper.getSimpleLetterDetailList(officialLetterId);
        List<AdminUserRespDTO> userRespVOS = new ArrayList<>();
        pageResult.forEach(detail -> {
            AdminUserRespDTO startUser = adminUserApi.getUser(detail.getUserId()).getCheckedData();
            if (startUser != null) {
                userRespVOS.add(startUser);
            }
        });

        return success(com.todod.base.framework.common.util.object.BeanUtils.toBean(userRespVOS, UserRespVO.class));
    }

    @Override
    public CommonResult<List<FileRespVO>> getLetterFileList(Long officialLetterId) {
        List<BaseOfficialLetterFileDO> pageResult = baseOfficialLetterFileMapper.getSimpleLetterFileList(officialLetterId);
        List<FileDO> fileVOS = new ArrayList<>();
        pageResult.forEach(detail -> {
            FileDO file = fileMapper.selectById(detail.getFileId());
            if (file != null) {
                file.setUid(file.getId());
                fileVOS.add(file);
            }
        });
        return success(com.todod.base.framework.common.util.object.BeanUtils.toBean(fileVOS, FileRespVO.class));
    }
}
